package com.cch.demo.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cch.demo.sys.domain.Loginfo;
import com.cch.demo.sys.service.LoginfoService;
import com.cch.demo.sys.util.DataGridView;
import com.cch.demo.sys.util.ResultObj;
import com.cch.demo.sys.vo.LogInfoVo;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 华哥
 * @since 2020-03-07
 */
@RestController
@RequestMapping("logInfo")
public class LoginfoController {

    @Resource
    private LoginfoService loginfoServiceImpl;

    @RequestMapping("loadAllLogInfo")
    public DataGridView loadAllLogInfo(LogInfoVo logInfoVo){
        QueryWrapper<Loginfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(logInfoVo.getLoginname()),"loginname",logInfoVo.getLoginname());
        queryWrapper.like(StringUtils.isNotBlank(logInfoVo.getLoginip()),"loginip",logInfoVo.getLoginip());
        queryWrapper.ge(logInfoVo.getStartTime()!=null,"logintime",logInfoVo.getStartTime());
        queryWrapper.le(logInfoVo.getEndTime()!=null,"logintime",logInfoVo.getEndTime());
        queryWrapper.orderByDesc("logintime");
        IPage<Loginfo> page = new Page<>(logInfoVo.getPage(),logInfoVo.getLimit());
        loginfoServiceImpl.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @RequestMapping("deleteLoginfo")
    public ResultObj deleteLogininfo(Integer id){
        try{
            loginfoServiceImpl.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @RequestMapping("batchDeleteLoginfo")
    public ResultObj BatchDeleteLoginInfo(LogInfoVo logInfoVo){
        try{
            List<Serializable>  idlist = new ArrayList<>();
            for (Integer id:logInfoVo.getIds()){
                idlist.add(id);
            }
            loginfoServiceImpl.removeByIds(idlist);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}

